<?php
namespace Club\Controller;
use Think\Controller;
class CoachController extends PublicController {
    /*教练列表 开发人：周弈男*/
    public function coachlist(){
    	$coach = M('coach');
        $coach=$coach->join('club ON club.clubid = coach.club')->select();
    	$this->assign('coach',$coach);
    	$this->display();
    }
    /*添加教练 开发人：周弈男*/
    public function add(){
        $coach = M('coach');
        if($coach->create()){
            if ($_FILES['pic']['error'] == 0) {
                $info = $this->upload();
                $name=$info['pic']['savename'];
            } else {
                $name = "2.jpg";
            }
            $coach->pic=$name;
            $coach->grade='0';
            $coach->club=$_SESSION['club'];
            if (false !== $coach->add()) {
                $this->success('添加成功！', '');
            } else {         
                $this->error('数据错误','');           
            }
        }else {            
            $this->error($coach->getError());  
        }
    }
    /*实例化上传类 开发人：周弈男*/
    public function upload(){
        $upload = new \Think\Upload();
        $upload->maxSize = 3145728 ;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath = './Public/photo/';
        $upload->autoSub=false;
        $info = $upload->upload();

        if(!$info) {        
            $this->error($upload->getError());    
        }else{        
           return $info; 
        }                    
    }
    /*删除教练 开发人：周弈男*/
    public function delete(){
        $id=$_GET['coachid'];
        $coach=M('coach');
        $pic=$coach->where("coachid=$id")->find();
        unlink(realpath("./Public/photo/".$pic["pic"]));
        $coach=$coach->where("coachid=$id")->delete();
        if($coach=1){
            $this -> success('删除成功！','');
        }else{
            $this->error('删除失败！','');
        }
    }
    /*显示申请大师页 开发人：周弈男*/    
    public function applymaster(){
    	$id=$_GET['coachid'];
    	$coach=M('coach');
        $club=M('club');
    	$coach=$coach->where("coachid=$id")->find();
        $map['clubid']=$coach['club'];
        $club=$club->where($map)->find();
    	$this->assign(array('coach'=>$coach,'club'=>$club));
    	$this->display();
    }
    /*申请大师 开发人：周弈男*/ 
   	public function apply(){
   		$id=$_GET['coachid'];
   		$content=I('post.content');
   		$coach = M('coach');
		$apply = M("apply");
		$re = $coach->where("coachid=$id")->find();
		$where['coach']=$re['coachname'];
        $where['club']=$re['club'];
        $where['course']=$re['course'];
        $where['content']=$content;
		if (false !== $apply->add($where)) {
            $this->success('申请成功！', '');
        } else {         
            $this->error('数据错误','');           
        }
   	}
}
